var PregDueAssistant = Class.create(AbstractModule, {
// added by Janni Kovacs 8/20/9
	setupWidgets: function() {

		//About
		this.controller.setupWidget(Mojo.Menu.appMenu, {
			omitDefaultItems: true
		}, {
			visible: true,
			items: [
      		Mojo.Menu.editItem,
			{ label: "Disclaimer", command: "do-disclaimer" },					
				{ label: "License", command: "do-license" },
				{ label: "Help", command: "do-help" }
			]
		});


		this.controller.setupWidget("datePicker", {
			label: " "
		}, this.dateModel = { date: new Date()});
		this.textModel = {
			age: "",
			due: "",
			concept: "",
			disabled: true
		};
		this.controller.setupWidget("gestationalAge", { modelProperty: "age"}, this.textModel);
		this.controller.setupWidget("dueDate", { modelProperty: "due" }, this.textModel);
		this.controller.setupWidget("conceptDate", { modelProperty: "concept" }, this.textModel);

		this.controller.listen("datePicker", Mojo.Event.propertyChange, this.calculate.bindAsEventListener(this));
	},

	calculate: function() {
		var lmp = this.dateModel.date;
		var today = new Date();

		if (lmp > today) {
			return ("Oops. The LMP shouldn't be after today. Did you enter the year correctly?");
		}

		var edd = new Date(lmp.getTime() + 40 * 7 * 24 * 60 * 60 * 1000);
//		edd = edd.toLocaleString("ddd M/d/yyyy");
		edd = edd.toLocaleDateString("ddd M/d/yyyy");
		var conception = new Date(lmp.getTime() + 14 * 24 * 60 * 60 * 1000);
//		conception = conception.toLocaleString("ddd M/d/yyyy");
		conception = conception.toLocaleDateString("ddd M/d/yyyy");
		var ega = today - lmp;
		ega = ega / 86400000;


		var weeks = parseInt(ega / 7);

		if (weeks > 42) {
			return ("Oops. The pregnancy is past 42 weeks. Is the LMP is wrong? Please check.");
		}

		var days = ega % 7;
		days = Math.round(days);
		var result;

		if (days == 7) {
			weeks = weeks + 1;
			result = weeks + ' weeks exactly';
		} else {
			result = weeks + ' weeks and ' + days + '/7 days';
		}

		this.textModel.age = result;
		this.textModel.due = edd;
		this.textModel.concept = conception;
		this.controller.modelChanged(this.textModel, this);
		return null;
	}
});

function ref() {
	return "Calculates Pregnancy Due Dates based on the LMP";
}